﻿using DAL;
using IBLL;
using IDAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    
    public class FrStudentManage_BLL : IFrStudentManage_IBLL
    {
        IFrStudentManage_IDAL  Manage_IDAL = new FrStudentManage_DAL();
        //添加考生信息
        int IFrStudentManage_IBLL.AddSStudent(Students_MD Students)
        {
            //验证年龄
            Students.Age = SQLHepler.GetDateTime().Year - Convert.ToDateTime(Students.Birthday).Year; 
            if (Students.Age > 30 || Students.Age < 18)
            {
                return -1;
            }
            if (SQLHepler.GetScalar($"select StudentIdNo from Examinee_info where StudentIdNo = {Students.StudentIdNo}") != null)
            {
                return -1;
            }
            try
            {
                return Manage_IDAL.AddStudent(Students);
            }
            catch (Exception ex)
            {

                throw ex;
            }
            
        }
        //批量导入考生信息  添加到数据库
        bool IFrStudentManage_IBLL.AddStudentsFromExcel(List<Students_MD> Students)
        {
            string str = " insert into Examinee_info(StudentName,Gender,Birthday,StudentIdNo,PhoneNumber,StudentAddress ,ClassId) values('{0}','{1}','{2}','{3}','{4}','{5}',{6}); \r\b";
            string str1 = "";
            foreach (Students_MD row in Students)
            {
                str1 += string.Format(str, row.StudentName, row.Gender, row.Birthday,
                  row.StudentIdNo, row.PhoneNumber, row.StudentAddress, row.ClassId);
            }
            try
            {
                
               return DAL.SQLHepler.UpdateByTransaction(str1);
               
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //修改考生信息
        bool IFrStudentManage_IBLL.AlterStudent(Students_MD Students, string id)
        {
            try
            {
                string i = SQLHepler.GetScalar($"select StudentIdNo from Examinee_info where StudentIdNo = {Students.StudentIdNo}")!=null?
                    SQLHepler.GetScalar($"select StudentIdNo from Examinee_info where StudentIdNo = {Students.StudentIdNo}").ToString() :null;
                if (i!=null)
                {
                    if (i!=Students.StudentIdNo)
                    {
                        return false;
                    }
                }
                return Manage_IDAL.AlterStudent(Students, id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //批量导入考生信息查看
        List<Students_MD> IFrStudentManage_IBLL.GetStudentsFromExcel(string path)
        {
            try
            {
                return Manage_IDAL.GetStudentsFromExcel(path);
            }
            catch (Exception ex)
            {

                throw ex;
            }
           
        }
        /// <summary>
        ///  科目
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        List<Course_info_MD> IFrStudentManage_IBLL.SCourse_info()
        {
            return Manage_IDAL.SCourse_info();
        }
        //班级信息
        List<StudentClass_MD> IFrStudentManage_IBLL.SClass()
        {
          return  Manage_IDAL.Sclass();
        }
        //查询全校考生信息
        List<Students_MD> IFrStudentManage_IBLL.Students()
        {
            return Manage_IDAL.Students("");
        }
        //查询考生信息
        List<Students_MD> IFrStudentManage_IBLL.Students(string path)
        {
            return Manage_IDAL.Students(path);
        }
        #region 删除
        void IFrStudentManage_IBLL.RemoveStudent(string sql)
        {
            DAL.SQLHepler.UpdateByTransaction(sql);
        }

     
        #endregion
    }
}
